package com.zmm.myyimixian.ui.activity.address;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.zmm.myyimixian.R;
import com.zmm.myyimixian.base.BaseActivity;
import com.zmm.myyimixian.db.Address;
import com.zmm.myyimixian.db.DataDao;
import com.zmm.myyimixian.util.TextUtils;
import com.zmm.myyimixian.widget.SelfDialog;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.listener.DeleteListener;
import cn.bmob.v3.listener.UpdateListener;

public class ModificationAddressActivity extends BaseActivity {


    @BindView(R.id.modification_address_toolbar)
    Toolbar mAddressToolbar;//toolbar
    @BindView(R.id.modification_address_name)
    EditText mAddressName;//名字
    @BindView(R.id.modification_address_sex_nv)
    RadioButton mAddressSexNv;//性别女
    @BindView(R.id.modification_address_sex_nan)
    RadioButton mAddressSexNan;//性别男
    @BindView(R.id.modification_address_phone)
    EditText mAddressPhone;//手机号
    @BindView(R.id.modification_address_province)
    TextView mAddressProvince;//省份
    @BindView(R.id.modification_address_city)
    TextView mAddressCity;//城市
    @BindView(R.id.modification_address_district)
    TextView newAddressDistrict;//区县
    @BindView(R.id.modification_address_detailed)
    EditText mAddressDetailed;//详细地址
    @BindView(R.id.modification_address_building)
    EditText mAddressBuilding;//楼号
    @BindView(R.id.modification_address_tag)
    EditText mAddressTag;//标签
    private Address address;//数据表对象
    private String sex = "女士";

    @Override
    protected void initView() {
        address = (Address) getIntent().getSerializableExtra("address");
        sex = address.getUserSex();
        //绑定定位数据
        if (sex.equals("女士")) {
            mAddressSexNv.setChecked(true);
        } else {
            mAddressSexNan.setChecked(true);
        }
        mAddressName.setText(address.getUserName());
        mAddressPhone.setText(address.getUserPhone());
        mAddressProvince.setText(address.getUserProvince());
        mAddressCity.setText(address.getUserCity());
        newAddressDistrict.setText(address.getUserDistrict());
        mAddressDetailed.setText(address.getUserDetailedAddress());
        mAddressBuilding.setText(address.getUserBuilding());
        String userTag = address.getUserTag();
        if (!"".equals(userTag)) {//标签可能为空
            mAddressTag.setText(userTag);
        }
        //返回
        mAddressToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @OnClick({R.id.modification_address_save, R.id.modification_address_sex_nv, R.id.modification_address_sex_nan, R.id.modification_address_layout, R.id.modification_address_down, R.id.modification_address_delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.modification_address_save://保存按钮
                modificationAddress();//保存收货地址的方法
                break;
            case R.id.modification_address_sex_nv://女
                sex = mAddressSexNv.getText().toString();
                break;
            case R.id.modification_address_sex_nan://男
                sex = mAddressSexNan.getText().toString();
                break;
            case R.id.modification_address_layout://跳转到选择地址
                startActivityForResult(new Intent(this, WheelActivity.class), 1);
                break;
            case R.id.modification_address_down://弹出选择标签
                showAlertDialog();
                break;
            case R.id.modification_address_delete://删除该地址
                //删除该地址
                showDialog();
                break;
        }
    }

    private void showDialog() {
        //自定义dialog
        final SelfDialog selfDialog = new SelfDialog(ModificationAddressActivity.this);
        selfDialog.setMessage("确定要删除这个地址吗？");
        selfDialog.setYesOnclickListener("确定", new SelfDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                selfDialog.dismiss();
                deleteAddress();
            }
        });
        selfDialog.setNoOnclickListener("取消", new SelfDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                selfDialog.dismiss();
            }
        });
        selfDialog.show();
    }

    //删除地址
    private void deleteAddress() {
        DataDao.deleteAddress(address, new DeleteListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(ModificationAddressActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(ModificationAddressActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
            }
        });
    }

    //显示alertDialog
    private void showAlertDialog() {
        final String[] arrayFruit = new String[]{"单位", "家", "学校", "其他"};
        new AlertDialog.Builder(this).
                setTitle("选择标签").
                setItems(arrayFruit, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mAddressTag.setText(arrayFruit[which]);
                    }
                }).setCancelable(true).show();
    }

    private void modificationAddress() {
        //收货人姓名返回
        if ("".equals(mAddressName.getText().toString().trim())) {
            Toast.makeText(this, "收货人姓名为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //手机号为空返回
        if ("".equals(mAddressPhone.getText().toString().trim())) {
            Toast.makeText(this, "手机号为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //详细地址为空返回
        if ("".equals(mAddressDetailed.getText().toString().trim())) {
            Toast.makeText(this, "详细地址为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //楼号为空返回
        if ("".equals(mAddressBuilding.getText().toString().trim())) {
            Toast.makeText(this, "请输入门牌号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!TextUtils.isMobileNO(mAddressPhone.getText().toString().trim())) {
            Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
            return;
        }
        //添加数据
        address.setUserName(mAddressName.getText().toString().trim());
        address.setUserSex(sex);
        address.setUserProvince(mAddressProvince.getText().toString());
        address.setUserCity(mAddressCity.getText().toString());
        address.setUserDistrict(newAddressDistrict.getText().toString());
        address.setUserDetailedAddress(mAddressDetailed.getText().toString().trim());
        address.setUserBuilding(mAddressBuilding.getText().toString());
        address.setUserTag(mAddressTag.getText().toString());
        address.setUserPhone(mAddressPhone.getText().toString());
        setAddress();
    }

    private void setAddress() {
        DataDao.setAddress(address, new UpdateListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(ModificationAddressActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(ModificationAddressActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
            }
        });
    }

    //接受回传的地址
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            mAddressProvince.setText(data.getStringExtra("province"));
            mAddressCity.setText(data.getStringExtra("city"));
            newAddressDistrict.setText(data.getStringExtra("district"));

        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_modification_address;
    }
}
